package com.iuschool;

import com.iuschool.controller.AwareController;
import com.iuschool.controller.WelcomeController;
import com.iuschool.entity.User;
import com.iuschool.entity.factory.UserFactoryBean;
import com.iuschool.service.WelcomeService;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * @description:
 * @author: max
 * @date: 2020/9/7 4:53 下午
 */
@Configuration
@ComponentScan("com.iuschool")
public class Entrance {
	public static void main1(String[] args) {
		String xmlPath = "//Users/max/working/java-project/open-source/spring-framework-5.2.8.RELEASE/springdemo/src/main/resources/spring/spring-config.xml";
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
		WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeService");
		welcomeService.sayHello("max");
	}

	public static void main2(String[] args) {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class);
		String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
		for (String beanDefinitionName : beanDefinitionNames) {
			System.out.println(beanDefinitionName);
		}
		WelcomeService welcomeService = (WelcomeService) applicationContext.getBean("welcomeServiceImpl");
		welcomeService.sayHello("spring框架无敌");
		WelcomeController controller = (WelcomeController) applicationContext.getBean("welcomeController");
		controller.handleRequest();
	}

	public static void main3(String[] args) {
		String xmlPath = "//Users/max/working/java-project/open-source/spring-framework-5.2.8.RELEASE/springdemo/src/main/resources/spring/spring-config.xml";
		ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath);
		// 相同的对象
		User u1A = (User) applicationContext.getBean("user1");
		User u1B = (User) applicationContext.getBean("user1");
		System.out.println("无参数构造函数创建的对象：" + u1A);
		System.out.println("无参数构造函数创建的对象：" + u1B);

		// 不同的对象
		User u2A = (User) applicationContext.getBean("user2");
		User u2B = (User) applicationContext.getBean("user2");
		System.out.println("静态工厂创建的对象：" + u2A);
		System.out.println("静态工厂创建的对象：" + u2B);

		// 默认scope是singleton,相同的对象
		User u3A = (User) applicationContext.getBean("user3");
		User u3B = (User) applicationContext.getBean("user3");
		System.out.println("实例工厂创建的对象：" + u3A);
		System.out.println("实例工厂创建的对象：" + u3B);

		// FactoryBean 能够制造 bean 的 备案
		User u4A = (User) applicationContext.getBean("userFactoryBean");
		User u4B = (User) applicationContext.getBean("userFactoryBean");
		System.out.println("FactoryBean 创建的对象：" + u4A);
		System.out.println("FactoryBean 创建的对象：" + u4B);

		// 获取 FactoryBean 实例
		FactoryBean<User> factoryBean = (UserFactoryBean) applicationContext.getBean("&userFactoryBean");
		System.out.println("通过 & 符取 FactoryBean：" + factoryBean);
	}

	public static void main4(String[] args) {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class);
		User user5 = (User) applicationContext.getBean("user5");
		System.out.println("CustomizedBeanDefinitionRegistryPostProcessor创建的对象: " + user5);
	}

	public static void main(String[] args) {
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class);
		AwareController awareController = (AwareController)applicationContext.getBean("awareController");
		awareController.handleRequest();
	}
}
